/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.DateTools;
import javacommon.util.StringUtils;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.pub.dao.AttendanceDao;
import com.tech15.pub.model.Attendance;
import com.tech15.pub.vo.query.AttendanceQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class AttendanceManager extends BaseManager<Attendance,Long>{

	private AttendanceDao attendanceDao;
	public void setAttendanceDao(AttendanceDao dao) {
		this.attendanceDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.attendanceDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(AttendanceQuery query) {
		return attendanceDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return attendanceDao.findByPageRequest(pr);
	}
	
	@Transactional(readOnly=true)
	public Page search(AttendanceQuery query) {
		return attendanceDao.search(query);
	}
	
	public void saveAtt(Attendance att) {
		Integer count = attendanceDao.getCount("select count(*) from attendance t where t.userid="+att.getUserid()+" and to_char(t.regtime,'yyyy-mm-dd')='"+att.getCurdate()+"'");
		if(count == 0){
			attendanceDao.save(att);
		}
	}
	
	public void savemyAtt(Attendance att) {
		/*Integer attid = attendanceDao.getCount("select max(id) from attendance t where t.isauto=0 and t.userid="+att.getUserid()+" and to_char(t.regtime,'yyyy-mm-dd')='"+att.getCurdate()+"'");
		if(ObjectUtils.isNotEmpty(attid)){
			Attendance attendance = attendanceDao.getById(new Long(attid));
			attendance.setIsauto(1);
			attendanceDao.update(attendance);
		}*/
		
		Integer count = attendanceDao.getCount("select count(*) from attendance t where t.userid="+att.getUserid()+" and to_char(t.regtime,'yyyy-mm-dd')='"+att.getCurdate()+"'");
		if(count == 0){
			att.setRegtime(DateTools.getCurrentTime());
			att.setIsauto(1);
			attendanceDao.save(att);
		}else{
			Integer attid = attendanceDao.getCount("select max(id) from attendance t where t.userid="+att.getUserid()+" and to_char(t.regtime,'yyyy-mm-dd')='"+att.getCurdate()+"'");
			if(ObjectUtils.isNotEmpty(attid)){
				Attendance attendance = attendanceDao.getById(new Long(attid));
				attendance.setIsauto(1);
				attendanceDao.update(attendance);
			}
		}
	}
	
	public Integer getIfqd(Long userid,String curday){
		Integer count = attendanceDao.getCount("select count(*) from attendance t where t.userid="+userid+" and to_char(t.regtime,'yyyy-mm-dd')='"+curday+"'");
		return count;
	}
	public InputStream searchExport(AttendanceQuery query) {
		List<Attendance> list = attendanceDao.searchExport(query);
		if(list != null && list.size()>0){
			ByteArrayOutputStream ops = genXls(list);
			InputStream ips = new ByteArrayInputStream(ops.toByteArray()); 
			return ips;
		}
		return null;
	}
	
	public ByteArrayOutputStream genXls(List<Attendance> ycjlList){
		WritableWorkbook wwb;
		ByteArrayOutputStream ops = new ByteArrayOutputStream();
        try {
	        wwb = Workbook.createWorkbook(ops);
	        //设置单元格的文字格式
	        WritableFont wf = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
	        WritableCellFormat wcf = new WritableCellFormat(wf);
	        wcf.setVerticalAlignment(VerticalAlignment.CENTRE); 
	        wcf.setAlignment(Alignment.CENTRE); 
	        //填充数据的内容
	        Label label;
	        WritableSheet sheet=wwb.createSheet("考勤月度报表", 0);
	        
	        sheet.setColumnView(0,20);
	        sheet.setColumnView(1,20);
	        sheet.setColumnView(2,20);
	        sheet.setColumnView(3,20);
	        sheet.setColumnView(4,20);
	        sheet.setColumnView(5,20);
	        //创建表头
			//sheet.mergeCells(0,0,0,0);  
	        label = new Label(0,0,"序号",wcf);   
	        sheet.addCell(label); 
	        label = new Label(1,0,"工号",wcf);   
	        sheet.addCell(label); 
	        label = new Label(2,0,"姓名",wcf);   
	        sheet.addCell(label);
	        label = new Label(3,0,"部门",wcf);   
	        sheet.addCell(label);
	        label = new Label(4,0,"在岗天数",wcf);   
	        sheet.addCell(label);
	        label = new Label(5,0,"餐补金额",wcf);   
	        sheet.addCell(label);
	        
	        int i=0;
	        for (Attendance jl : ycjlList){
	        	label = new Label(0,i+1,(i+1)+"",wcf);   //序号
	        	sheet.addCell(label);
	            label = new Label(1,i+1,jl.getYgbh(),wcf);   
            	sheet.addCell(label);
            	label = new Label(2,i+1,jl.getName(),wcf);   		
	            sheet.addCell(label);
	            label = new Label(3,i+1,jl.getDeptname(),wcf);   		
	            sheet.addCell(label);
	            label = new Label(4,i+1,jl.getZgts()+"",wcf);   	
	            sheet.addCell(label);
	            label = new Label(5,i+1,jl.getCbje()+"",wcf);   			
	            sheet.addCell(label);
	            i++;
	       }
	       wwb.write();
	       wwb.close();
       } catch (IOException e){
        	e.printStackTrace();
       } catch (RowsExceededException e){
        	e.printStackTrace();
       } catch (WriteException e){
        	e.printStackTrace();
       }
       return ops;
	}
	public Integer getQdZs(Long userid, String displayDate) {
		if(StringUtils.notNull(displayDate)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			displayDate = sdf.format(new Date()).toString();
		}
		Integer count = attendanceDao.getCount("select count(*) from attendance t where t.userid="+userid+" and to_char(t.regtime,'yyyy-mm')='"+displayDate+"'");
		return count;
	}
	public void savemyGroup(Attendance attendance) {
		if(attendance.getExecUsers() != null){
			for(Long userid : attendance.getExecUsers()){
				Integer count = attendanceDao.getCount("select count(*) from attendance t where t.userid="+userid+" and to_char(t.regtime,'yyyy-mm-dd')='"+DateTools.getDisplayDate()+"'");
				if(count == 0){
					Attendance att = new Attendance();
					att.setUserid(userid);
					att.setRegtime(DateTools.getCurrentTime());
					att.setIsauto(1);
					attendanceDao.save(att);
				}
			}
		}
	}
	
	
	
}
